define([
    "dojo/_base/declare",
    "gistech/widgets/normalDialog/NormalDialog",
    "dojo/text!./templates/Template.html",
    "dojo/_base/lang",
    "dojo/dom-style",
    "dojo/dom-construct",
    "dojo/on",
    "dojo/_base/connect",
    "gistech/util/ajaxUtil",
    "dojo/dom-style","dojo/query","dojo/dom-class",
    "../UserSet/UserSet","dojo/topic"],
    function(declare,NormalDialog,template,lang,style,domConstruct,on,eventconnect,ajaxUtil,domStyle,query,domClass,UserSet,topic){
        return declare([NormalDialog],{
        	
            templateString:template,
            container:null,
            resetAttr:function(){
                this.inherited(arguments);
            },

            constructor:function (args) {
            	this.inherited(arguments);
            	this.container = args.container;
            	this.companyNode=null,
            	this.businessNode=null,
            	this.developmentNode=null,
            	
            	//
               	this.allVehicleData = new Object();
                this.map = args.map;
                this.container = args.container;
                this.currentPage=1;
                this.totalPage=1;
                this.currentSource=[];
                this.companyOptionHTML = "<option selected=\"selected\" value=\"\">请选择</option>";
                this.departmentOptionHTML = "<option selected=\"selected\" value=\"\">请选择</option>";
                
                this.organizationName = args.organizationName;
  			    this.typeName = args.typeName,
			    this.parentName = args.parentName,
			    this.menagerName = args.menagerName,
			    this.organizationId = args.organizationId,
			    this.menagerId = args.menagerId,
			    this.parentId = args.parentId
            },
            
            postCreate:function(){
                this.inherited(arguments);
                //domConstruct.place(this.domNode, this.container);
                this.init();
                this.addEvents();

            },

            /**
             * 初始化
             */
            init:function () {
                this.companyNode=dojo.byId('company');
                this.businessNode=dojo.byId('business');
                this.developmentNode=dojo.byId('development');
            	this.loadData(true,true);
//            	this.getOrganizationData(0,1);
            },
            
            
            addEvents:function(){
//            	topic.subscribe("ChangeArrowDirection", lang.hitch(this, this.ChangeArrowDirection));
//            	on(dojo.byId('organizationName'),"keypress",dojo.hitch(this,this.nameKeyPress));
//            	on(this.organizationBtnOk,"click",dojo.hitch(this,this.loadData));
            	on(dojo.byId('organizationType'),"change",dojo.hitch(this,this.changeParentOrganization));//机构类型
            	on(this.organizationEditBtnOk,"click",dojo.hitch(this,this.saveOrganization));
                on(dojo.byId("menagerUserName"),"click",function(e){
                	var userSet = new UserSet(
                			{
                				pos:{l:Math.ceil(document.body.clientWidth)/2-419,t:110},
                				flag:"ChangeMenagerUserName"
                			},dojo.byId("dialog"));
                	userSet.setTitle("人员查询");
                	userSet.startup();
                });
                topic.subscribe("ChangeMenagerUserName", lang.hitch(this, this.ChangeMenagerUserName));

            },
            
            //所属机构
            changeParentOrganization:function(e){
            	var value  = dojo.byId('organizationType').value;
            	if(value==1){
                  dojo.byId("parentOrganization").innerHTML = "";
                }else if(value==2){
               	  dojo.byId("parentOrganization").innerHTML = this.companyOptionHTML;
                }else if(value==3){
                  dojo.byId("parentOrganization").innerHTML = this.departmentOptionHTML;
                }
            	
            },
            
            /*
             * 加载数据
             */
            loadData:function(bCanClick,bShowLoading){
                ajaxUtil.request("../client/service/userOrganizationController/getOrganization","parentId=",lang.hitch(this,function(result){
                     var Store = eval(result.result);
                     
                     for(var i=0; i<Store.length;i++){
                    	 if(Store[i].ORGANIZATION_TYPE==1){
                    		 this.companyOptionHTML += "<option value='"+Store[i].ID+"'>"+Store[i].ORGANIZATION_NAME+"</option>";
                    	 }else if(Store[i].ORGANIZATION_TYPE==2){
                    		 this.departmentOptionHTML += "<option value='"+Store[i].ID+"'>"+Store[i].ORGANIZATION_NAME+"</option>";
                    	 }
                     }
                     
                     //判断新增或修改
                     
                     if(this.organizationId!=null){
                    	 this.organName.value = this.organizationName;
                    	 dojo.byId("organizationType").value = this.typeName;
                    	 if(this.typeName==1){//集团
                             dojo.byId("parentOrganization").innerHTML = "";// 

                    	 }else if(this.typeName==2){//事业部
                             dojo.byId("parentOrganization").innerHTML = this.companyOptionHTML;// 

                    	 }else if(this.typeName==3){//拓展组
                             dojo.byId("parentOrganization").innerHTML = this.departmentOptionHTML;// 
                    	 }
                    	 dojo.byId("parentOrganization").value = this.parentId;
                    	 dojo.byId("menagerUserName").value= this.menagerName;
                     }else{
                    	 dojo.byId("parentOrganization").innerHTML = "";//默认集团
                     }
                }),function(){
                    console.error("获取下拉列表失败");
                 });
                    
            },
 
 
 

            saveOrganization:function(){
            	
            	var organizationName = dojo.byId("organName").value;
            	var organizationType = dojo.byId("organizationType").value;
            	var parentOrganization = dojo.byId("parentOrganization").value;
            	if(organizationType==1){
            		parentOrganization=0;
            	}
            	
            	var menagerUserId = this.menagerId;
            	if(organizationName==''){
            		alert('机构名称不能为空');
            		return false;
            	}
            	if(organizationType==''){
            		alert('机构类型不能为空');
            		return false;
            	}
            	var url = "";
            	var param="";
            	if(this.organizationId!=null && this.organizationId!=''){
            		url ="../client/service/userOrganizationController/updateOrganization"
            		param+=("organizationId="+this.organizationId+"&");
            	}else{
            		url ="../client/service/userOrganizationController/addOrganization"
            	}
            	  param+="organizationMenagerId="+menagerUserId+"&organizationName="+organizationName+"&organizationType="+organizationType+'&parentId='+parentOrganization;
            	
            	//
            	ajaxUtil.request(url,param,lang.hitch(this,function(result){
            		if(result.success){
            			this.loadData();
            			alert('操作成功')
            			topic.publish("loadOrganizationData");
            			this.inherited("closeDialog",arguments);
            			this.destroy();
            			
            		}else{
            			alert('操作失败');
            		}
            	}),function(){
            		alert('操作失败');
                    console.error("新增/编辑组织机构数据失败");
                });
            },
            
            
            ChangeMenagerUserName:function(userId,userName){
//            	alert("ChangeMenagerUserName")
            	this.menagerId =userId;
            	dojo.byId("menagerUserName").value = userName;
            },
            
            startup:function(){
                this.inherited(arguments);
            },
            closeDialog:function(){
    			this.inherited("closeDialog",arguments);
    			this.destroy();
    		}

    });
});

